/*
 * @Author: your name
 * @Date: 2021-01-14 10:42:58
 * @LastEditTime: 2021-01-14 16:28:29
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \carlist\src\store\reducers\cars.js
 */
import {GET_BRAND_LIST,GET_MAKE_LIST} from "../actionType";

const initstate={
    brandList:[],
    makeList:[]
}

/** 
 * brandList 想要的是这样
 *  [
                {
                    id:1,
                    FirstSpll:A,
                    []
                },
                {
                    id:2,
                    FirstSpell:B,
                    []
                }
            ]
*/
const cars = (state=initstate,action)=>{
    let newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case GET_BRAND_LIST:{
            //创建一个能够存储多个数据的结构;
            //获取已有的车系首字母组成的不重复的结构;
            let arr = new Set();
            action.data.forEach((v)=>{
                 arr.add(v.Spelling[0])
            })
            // [A,B,C,D...]
            // A===[A]
            //循环遍历一下
            newstate.brandList = Array.from(arr).map((v,i)=>{
                  let children=[];
                  action.data.map(item=>{
                      if(item.Spelling[0]===v){
                          children.push(item)
                      }
                  })

                  return {
                       id:i,
                       FirstSpell:v,
                       children
                  }
            })
        }
            break;
         case GET_MAKE_LIST:newstate.makeList = action.data
         break;
        default:
            break;
    }
    return newstate
}

export default cars